package edu.gatech.teamrocket;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.j256.ormlite.dao.Dao;

import edu.gatech.teamrocket.models.Category;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.Spinner;

public class FilterDialog extends Dialog {
	private DewApplication app;
	private Dao<Category, Integer> categoryDao;
	private List<Category> categories;
	// this is your interface for what you want to do on the calling activity
	public interface ReadyListener {
		public void ready(boolean filterByCategoryRes,
			boolean filterByDateRes, boolean filterByCompleteRes,
			Category category, Date date, boolean completed);
	}
	
	private ReadyListener onFilterDialogEventListener;
	
	// in the constructor, you set the callback 
	public FilterDialog(Context context, ReadyListener onCustomDialogEventListener) {
	    super(context);
	    this.app = (DewApplication) context.getApplicationContext();
	    this.onFilterDialogEventListener = onCustomDialogEventListener;
	}
	
	// And in onCreate, you set up the click event from your dialog to call the callback (ready)
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.filter_dialog);
        setTitle("Filter");
        final Button filterButton = (Button) findViewById(R.id.filterButton);
        final Button cancelButton = (Button) findViewById(R.id.cancelFilter);
        final Spinner categorySpinner = (Spinner) findViewById(R.id.categoryFilterValue);
        final Spinner completedSpinner = (Spinner) findViewById(R.id.completeFilterValue);
        
        try {
        	categoryDao = app.getHelper().getCategoryDao();
			categories = categoryDao.queryBuilder().where().eq("user_id", app.getAuth().getUser()).query();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
        
        final ArrayAdapter<Category> categoryAdapter = new ArrayAdapter<Category>(app, android.R.layout.simple_spinner_item, categories);
        categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        categorySpinner.setAdapter(categoryAdapter);
        final ArrayAdapter<String> completedAdapter = new ArrayAdapter<String>(app, android.R.layout.simple_spinner_item, new String[] {"Incomplete", "Completed"});
        completedAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        completedSpinner.setAdapter(completedAdapter);
        
	    Button.OnClickListener buttonListener = (new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.filterButton:
				    // get the status of each filter
				    final CheckBox categoryCheck = (CheckBox) findViewById(R.id.categoryFilterCheck);
				    final CheckBox dateCheck = (CheckBox) findViewById(R.id.dateFilterCheck);
				    final CheckBox completeCheck = (CheckBox) findViewById(R.id.completeFilterCheck);
				    // get the value of each filter
				    final Category categoryValue = (Category) categorySpinner.getSelectedItem();
				    final String completeValue = (String) completedSpinner.getSelectedItem();
				    final DatePicker datePicker = (DatePicker) findViewById(R.id.dateFilterValue);
				    final Date dateValue = new Date(datePicker.getYear() - 1900, datePicker.getMonth(), datePicker.getDayOfMonth());
				    final boolean completed = completeValue.equals("Completed") ? true : false;
					onFilterDialogEventListener.ready(categoryCheck.isChecked(), dateCheck.isChecked(), completeCheck.isChecked(), categoryValue, dateValue, completed);
		            dismiss();
		            break;
				case R.id.cancelFilter:
					dismiss();
					break;
				}
			}
	     });
        
        filterButton.setOnClickListener(buttonListener);
        cancelButton.setOnClickListener(buttonListener);
	 }
}
